package com.example.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.eduservice.entity.EduChapter;
import com.example.eduservice.entity.EduVideo;
import com.example.eduservice.entity.chapter.ChapterVo;
import com.example.eduservice.entity.chapter.VideoVo;
import com.example.eduservice.mapper.EduChapterMapper;
import com.example.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.eduservice.service.EduVideoService;
import com.example.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-11-04
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    //因为EduChapterServiceImpl中不能通过扫描配置文件，只能手动导入EduVideoService
    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> getChapterrVideoByCourseId(String courseId) {
        //根据课程id查询课程里所有的章节
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(chapterQueryWrapper);
        System.out.println("eduChapters(章节内容):" + eduChapters);
        //根据课程id查询课程里所有的小节
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoService.list(eduVideoQueryWrapper);
        System.out.println("eduVideos(小节内容):" + eduVideos);
        //遍历查询章节list集合进行封装
        ArrayList<ChapterVo> finalList = new ArrayList<>();
        for (int i = 0; i < eduChapters.size(); i++) {
            ChapterVo chapterVo = new ChapterVo();
            EduChapter eduChapter = eduChapters.get(i);
            //将eduChapter中的内容复制到chapterVo
            BeanUtils.copyProperties(eduChapter,chapterVo);
            System.out.println("验证复制是怎么复制的" + eduChapter);
            System.out.println(chapterVo);
            //最终放到finalList集合里
            finalList.add(chapterVo);
            //创建集合，用于最后进行一个封装
            ArrayList<VideoVo> voArrayList = new ArrayList<>();
            //遍历查询小节list集合，进行封装
            for (int m = 0; m < eduVideos.size();m++) {
                EduVideo eduVideo = eduVideos.get(m);
                System.out.println("验证小节的内容" + eduVideo);
                //判断：小节里面chapter_id和章节中的id是否一致
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    //章节和小节的id对应上了
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    //放到小节封装集合
                    voArrayList.add(videoVo);
                }
            }
            //把封装之后小节list  放到章节对象里面去
            chapterVo.setChildren(voArrayList);
        }

        return finalList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapterid章节id 查询小节表，如果查询数据，不进行删除
        //这里有个很新颖的点就是他查询的是根据这个课程id能查到多少的小节，如果查到小节了
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(queryWrapper);
        if (count > 0){
            //能查询到数据，不让他删除章节
            throw new GuliException(20001,"不能删除");
        }else {
            //查不到数据，让他删除
            int result = baseMapper.deleteById(chapterId);
            //成功  1>0   0>0
            return result > 0;
        }
        //判断
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
